package org.kineticsproject.application.util;

import java.io.File;
import java.io.IOException;

import data.DataFactory;
import data.DataPackage;
import data.Exercise;
import data.Person;
import data.Population;
import data.Sport;

import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
import org.eclipse.emf.ecore.xmi.impl.XMIResourceFactoryImpl;
import org.kineticsproject.devices.parsers.*;

public class PopulateExercises {

	/**
	 * @param args
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException {
		
		
		/* Import Device Data through Parsers */
		Exercise exercise = DataFactory.eINSTANCE.createExercise();
		exercise.setName("Grand Canyon");
		exercise.setSport(Sport.HIKING);
		
		PolarParser polarparser = new PolarParser();
		polarparser.process(exercise, "gcpolar.txt", "2005-10-5" , 0);
		
		GarminGPSParser garminparser = new GarminGPSParser();
		garminparser.process(exercise, "gcgps.txt", "2005-10-5", 0);
		
		
		
		
		/* Load Population */
		DataPackage dataPackage = DataPackage.eINSTANCE;
		
		//Create new ResourceSet & Resource
		ResourceSet resourceSet = new ResourceSetImpl();
		
		// Register XML resource factory
		resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put("xmi", 
		new XMIResourceFactoryImpl());

		Resource resource = resourceSet.createResource(URI.createFileURI(new File("myPopulation.xmi").getAbsolutePath()));

		// register package in local resource registry
		resourceSet.getPackageRegistry().put(dataPackage.getNsURI(), dataPackage);
		// load resource 
		resource.load(null);
		
		Population population = (Population)resource.getContents().get(0);

		
		Person casey = population.getPeople().get(0);
		
		casey.getExercises().add(exercise);
		
		
		
		
		/* Save Population */
		// serialize resource – you can specify also serialization
		// options which defined on org.eclipse.emf.ecore.xmi.XMIResource
		resource.save(null);
		
		
				
		

	}

}
